/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * zEditServiceList.java
 *
 * Created on Oct 31, 2011, 6:44:27 PM
 */
package introsemp;
import java.sql.*;
import javax.swing.table.*;
/**
 *
 * @author G
 */
public class Form_Terminals extends javax.swing.JFrame {

    /** Creates new form zEditServiceList */
    Form_MainMenu menu;
    DefaultTableModel table;
    public Form_Terminals(Form_MainMenu menuGet) {   
        initComponents();
        menu=menuGet;
        table=(DefaultTableModel)TerminalsTabel.getModel();
         try{
        PreparedStatement pstmt= menu.con.prepareStatement("SELECT * FROM terminals");
        ResultSet allTerminals= pstmt.executeQuery();
       while(allTerminals.next()){
           String num=allTerminals.getString("TerminalNumber");
           String status=allTerminals.getString("TerminalStatus");
           table.addRow(new Object[]{num,status});
        } 
       
       PreparedStatement pstmt2= menu.con.prepareStatement("SELECT * FROM rates");
        ResultSet getStatus=pstmt2.executeQuery();
        getStatus.next();
        String rate = getStatus.getString("rate");
        jLabel8.setText(rate);
        }
        catch(SQLException ex){
            System.out.println("nada");
        } 
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jDialog2 = new javax.swing.JDialog();
        jLabel4 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        TerminalSettings = new javax.swing.JLabel();
        Separator1 = new javax.swing.JSeparator();
        Separator3 = new javax.swing.JSeparator();
        Terminals = new javax.swing.JLabel();
        TerminalsTablePane = new javax.swing.JScrollPane();
        TerminalsTabel = new javax.swing.JTable();
        ChangeStatus = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();

        jDialog1.setResizable(false);

        jLabel1.setText("Are you sure you want to change the number of computer?");

        jButton2.setText("Ok");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Cancel");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setText("This will reset all current terminals activated");

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jLabel1)
                .addContainerGap(548, Short.MAX_VALUE))
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(84, 84, 84)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(589, Short.MAX_VALUE))
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(11, 11, 11)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addContainerGap(470, Short.MAX_VALUE))
        );

        jDialog2.setResizable(false);

        jLabel4.setText("Are you sure you want to change the number of computer?");

        jButton6.setText("Ok");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Cancel");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel5.setText("This will reset all current terminals activated");

        javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane());
        jDialog2.getContentPane().setLayout(jDialog2Layout);
        jDialog2Layout.setHorizontalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog2Layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jLabel4)
                .addContainerGap(548, Short.MAX_VALUE))
            .addGroup(jDialog2Layout.createSequentialGroup()
                .addGap(84, 84, 84)
                .addGroup(jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jDialog2Layout.createSequentialGroup()
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(589, Short.MAX_VALUE))
        );
        jDialog2Layout.setVerticalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog2Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addGap(11, 11, 11)
                .addGroup(jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton7)
                    .addComponent(jButton6))
                .addContainerGap(470, Short.MAX_VALUE))
        );

        TerminalSettings.setFont(new java.awt.Font("Tahoma", 0, 18));
        TerminalSettings.setText("Terminal Settings");

        Terminals.setFont(new java.awt.Font("Tahoma", 0, 14));
        Terminals.setText("Terminals:");

        TerminalsTabel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Terminal #", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TerminalsTabel.setShowVerticalLines(false);
        TerminalsTablePane.setViewportView(TerminalsTabel);

        ChangeStatus.setText("Change Status");
        ChangeStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeStatusActionPerformed(evt);
            }
        });

        jButton4.setText("Add Terminal");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Remove Terminal");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel7.setText("Rate per Hour: PHP");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("Change Rate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel8.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Separator3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                    .addComponent(Separator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(TerminalSettings))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Terminals))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton5))
                            .addComponent(TerminalsTablePane, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(ChangeStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(88, 88, 88))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(TerminalSettings)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Separator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Separator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(Terminals)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TerminalsTablePane, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(ChangeStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel6))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    try{
        Integer get=menu.table.getRowCount();
        String max=Integer.toString(get);
        PreparedStatement pstmt= menu.con.prepareStatement("delete from terminals where TerminalNumber =?");
        int i=1;
            pstmt.setString(i++,max);
        pstmt.executeUpdate();
        
        while(menu.table.getRowCount()!=0){
           menu.table.removeRow(0);
           table.removeRow(0);
       }
        
        PreparedStatement pstmt2= menu.con.prepareStatement("SELECT * FROM terminals");
        ResultSet allTerminals= pstmt2.executeQuery();
       while(allTerminals.next()){
           String num=allTerminals.getString("TerminalNumber");
           String status=allTerminals.getString("TerminalStatus");
           menu.table.addRow(new Object[]{num,status,0F});
           table.addRow(new Object[]{num,status,0});
        }
    }
        catch(SQLException ex){
            System.out.println("nada");
        }
    
    this.jDialog1.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
    this.jDialog1.setVisible(false);
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
     this.jDialog2.setVisible(true);
     jDialog2.setSize(425, 150);
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
    try{
        System.out.println("Add");
        Integer get=menu.table.getRowCount()+1;
        String max=Integer.toString(get);
        PreparedStatement pstmt = menu.con.prepareStatement("INSERT INTO `terminals` (`TerminalNumber`, `TerminalStatus`) VALUES (?, ?)");      
            //append values for the parameters
            int i=1;
            pstmt.setString(i++, max);
            pstmt.setString(i++, "Available");
            pstmt.executeUpdate();
        
        while(menu.table.getRowCount()!=0){
           menu.table.removeRow(0);
           table.removeRow(0);
       }
        
        PreparedStatement pstmt2= menu.con.prepareStatement("SELECT * FROM terminals");
        ResultSet allTerminals= pstmt2.executeQuery();
       while(allTerminals.next()){
           String num=allTerminals.getString("TerminalNumber");
           String status=allTerminals.getString("TerminalStatus");
           menu.table.addRow(new Object[]{num,status,0F});
           table.addRow(new Object[]{num,status,0});
        }
    }
        catch(SQLException ex){
            System.out.println("nada");
        }
    
    this.jDialog2.setVisible(false);
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
// TODO add your handling code here:
    this.jDialog2.setVisible(false);
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
    this.jDialog1.setVisible(true);
     jDialog1.setSize(425, 150);
}//GEN-LAST:event_jButton5ActionPerformed

private void ChangeStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeStatusActionPerformed
// TODO add your handling code here:
    try{
        System.out.println("ChangeStatus");
        Integer selected=TerminalsTabel.getSelectedRow()+1;
        String selectedString=Integer.toString(selected);
        PreparedStatement pstmt= menu.con.prepareStatement("SELECT * FROM terminals where TerminalNumber = ?");
         int i=1;
        pstmt.setString(i++,selectedString);
        ResultSet getStatus=pstmt.executeQuery();
        getStatus.next();
        String status = getStatus.getString("TerminalStatus");
        System.out.println(status);
        if(status.equals("Available")){
        PreparedStatement pstmt2 = menu.con.prepareStatement("UPDATE terminals SET TerminalStatus='Disabled' WHERE TerminalNumber=?");  
        i=1;
        pstmt2.setString(i++,selectedString);
        pstmt2.executeUpdate();
        TimerComponent timer = (TimerComponent)menu.timerComponents.get(selected);
        timer.disable();
        }
        else{
        PreparedStatement pstmt2 = menu.con.prepareStatement("UPDATE terminals SET TerminalStatus='Available' WHERE TerminalNumber=?");      
        i=1;
        pstmt2.setString(i++,selectedString);
        pstmt2.executeUpdate();
        TimerComponent timer = (TimerComponent)menu.timerComponents.get(selected);
        timer.enable();
        }
        
        while(table.getRowCount()!=0){
           table.removeRow(0);
       }
        
        PreparedStatement pstmt3= menu.con.prepareStatement("SELECT * FROM terminals");
        ResultSet allTerminals= pstmt3.executeQuery();
       while(allTerminals.next()){
           String num=allTerminals.getString("TerminalNumber");
           String status1=allTerminals.getString("TerminalStatus");
           table.addRow(new Object[]{num,status1,0F});
        }
            
        }
         
    
        catch(SQLException ex){
            System.out.println("nada");
        }
    
}//GEN-LAST:event_ChangeStatusActionPerformed

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
   boolean check=false;
    try  
   {  
      Integer.parseInt(jTextField1.getText());  
      check = true;  
   }  
   catch(Exception e)  
   {  
      check = false;  
   }  
    if(check){
     try{
    PreparedStatement pstmt2 = menu.con.prepareStatement("UPDATE Rates SET rate=? WHERE idRates=1");  
        int i=1;
        pstmt2.setString(i++,(String)jTextField1.getText());
        pstmt2.executeUpdate();
        PreparedStatement pstmt3= menu.con.prepareStatement("SELECT * FROM rates");
        ResultSet getStatus=pstmt3.executeQuery();
        getStatus.next();
        String rate = getStatus.getString("rate");
        jLabel8.setText(rate);
       
     }
     catch(SQLException ex){
            System.out.println("nada");
        }
    }
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        
        /* Create and display the form */
      

           
     
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ChangeStatus;
    private javax.swing.JSeparator Separator1;
    private javax.swing.JSeparator Separator3;
    private javax.swing.JLabel TerminalSettings;
    private javax.swing.JLabel Terminals;
    private javax.swing.JTable TerminalsTabel;
    private javax.swing.JScrollPane TerminalsTablePane;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
